package com.zzzzzz.oms.wave;

import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import org.springframework.web.util.WebUtils;
import com.zzzzzz.core.datagrid.BaseData;
import com.zzzzzz.core.datagrid.BaseQueryForm;
import com.zzzzzz.utils.page.Page;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.zzzzzz.oms.GCS;
import com.google.common.collect.Lists;
import com.zzzzzz.plugins.shiro.ShiroUtils;
import com.zzzzzz.plugins.poi.ExportExcel;
import com.zzzzzz.plugins.poi.ImportExcel;

/**
 * @author hing
 * @version 1.0.0
 */
@Controller
public class WaveWeb {

	@Resource
	public WaveService waveService;
	
	@Resource
	public WaveDao waveDao;

	// 打开界面
	@RequestMapping(value = "/wave/list", method = RequestMethod.GET)
	public String list() {
		return "oms/wave";
	}

	// 增加、修改
	@RequestMapping(value = "/wave/save", method = RequestMethod.POST)
	@ResponseBody
	public BaseData save(@RequestBody @Valid Wave wave, BindingResult bindingResult) throws Exception {
		if (bindingResult.hasErrors()) {
			return new BaseData(GCS.ERRCD_PARAM);
		}
		waveService.save(wave, ShiroUtils.findUser());
		return new BaseData("0");
	}
	
	// 停用数据
	@RequestMapping(value = "/wave/updStByIds", method = RequestMethod.POST)
	@ResponseBody
	public BaseData updStByIds(@RequestParam(value = "ids", required = true) List<Long> ids, @RequestParam(value = "st", required = true) Integer st) throws Exception {
		waveDao.updStByIds(ids, st, ShiroUtils.findUser());
		return new BaseData("0");
	}	  
	
	// 查找一条数据
	@RequestMapping(value = "/wave/id/{id}", method = RequestMethod.GET)
	@ResponseBody 
	public BaseData findById(@PathVariable Long id) {
		Wave wave = waveDao.findById(id);
		return new BaseData(wave, null);
	}
	
	// 动态查询
	@RequestMapping(value = "/wave/findListBy", method = RequestMethod.POST)
	@ResponseBody
	public BaseData findListBy(@RequestBody BaseQueryForm baseQueryForm) {
		Page page;
		List<Wave> list;
		BaseData baseData;
		if(baseQueryForm.getPageNb()==null&&baseQueryForm.getPageSize()==null){
			list = waveDao.findListBy(null,baseQueryForm.getFfMap(),ShiroUtils.findUser());
			baseData=new BaseData(list, null);
		}else{
			page=baseQueryForm.getPage();
			list = waveDao.findListBy(page,baseQueryForm.getFfMap(),ShiroUtils.findUser());
			baseData=new BaseData(list, page.getTotal());
		}
		return baseData;
	}
    
	// 导出
    @RequestMapping(value = "/wave/export")
    public String exportFileTemplate(HttpServletRequest request, HttpServletResponse response, RedirectAttributes redirectAttributes) {
		try {
			Map<String, Object> params = WebUtils.getParametersStartingWith(request, null);
            String fileName = "波次信息数据.xlsx";            
            List<Wave> list =waveDao.findListBy(null, params,ShiroUtils.findUser());  
    		new ExportExcel("波次信息数据", Wave.class, 1).setDataList(list).write(response, fileName).dispose();
    		return null;
		} catch (Exception e) {
			redirectAttributes.addFlashAttribute("msg", "波次信息数据导出失败。");
			logger.warn("importFileTemplate", e);
		}
		return "redirect:/msg";
    }
    private static final Logger logger = LoggerFactory.getLogger(WaveWeb.class);
}
